package com.share.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.utils.StringUtils;
import com.share.device.domain.Region;
import com.share.device.mapper.RegionMapper;
import com.share.device.service.IRegionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {


    @Override
    public List<Region> treeSelect(String code) {
        List<Region> regions = baseMapper.selectList(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, code));
        if (!CollectionUtils.isEmpty(regions)){
            for (Region region : regions) {
                boolean exists = this.lambdaQuery().eq(Region::getParentCode, region.getCode()).exists();
                if (exists){
                    region.setHasChildren(true);
                }else {
                    region.setHasChildren(false);
                }
            }
        }
        return regions;
    }

    @Override
    public String getNameByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return "";
        }
        Region region = baseMapper.selectOne(new LambdaQueryWrapper<Region>().eq(Region::getCode,code).select(Region::getName));
        if(null != region) {
            return region.getName();
        }
        return "";
    }
}
